package com.googlecode.afdbaccess.web.touch.webcontent.js.common_login_data;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.SecurityContext;

import org.jboss.resteasy.annotations.cache.Cache;

import com.googlecode.afdbaccess.web.common.interceptors.RequestLogged;
import com.googlecode.afdbaccess.web.common.login.ApplicationPrincipal;

@RequestLogged
@Path("js/common-login-data")
public class LoginGetRest {
    @GET
    @Cache(isPrivate=true, maxAge=0)
    @Produces("application/json")
    public String get(@SuppressWarnings("unused") @Context HttpServletRequest request, @Context SecurityContext securityContext) {
//        fakeLogin(securityContext, request, "test", "testpw");
        
        String jsonString = "";
        jsonString += "{\n";
        jsonString += "  \"loggedIn\": " + (securityContext.getUserPrincipal() != null);
        if (securityContext.getUserPrincipal() != null) {
            ApplicationPrincipal applicationPrincipal = (ApplicationPrincipal)securityContext.getUserPrincipal(); 
            jsonString += ",\n";
            jsonString += "  \"userId\": \"" + applicationPrincipal.getUserId() + "\",\n";
            jsonString += "  \"userName\": \"" + applicationPrincipal.getName() + "\",\n";
            jsonString += "  \"displayName\": \"" + applicationPrincipal.getDisplayName() + "\",\n";

            String hasRoles = getHasRoles(securityContext);
            jsonString += "  \"hasRoles\": " + hasRoles + "\n";
        } else {
            jsonString += "\n";
        }
        jsonString += "}";
        
//        String nullString = null; nullString.toCharArray();
        
        return "{\"success\":\"true\", \"data\":" + jsonString + "}";
    }

    @SuppressWarnings("unused")
    private void fakeLogin(SecurityContext securityContext, HttpServletRequest request, String username, String password) {
        if(securityContext.getUserPrincipal() == null) {
            try {
                request.logout();
                request.login(username, password);
            } catch (ServletException exception) {
                throw new RuntimeException("Couldn't mock login.", exception);
            }
        }
    }

    private String getHasRoles(SecurityContext securityContext) {
        String[] allRoles = { "theRole" };
        
        String returnString = "";
        returnString += "[";
        
        for(String potentialRole: allRoles) {
            if(securityContext.isUserInRole(potentialRole)) {
                if(returnString.equals("[") == false) {
                    returnString += ", ";
                }
                returnString += "\"" + potentialRole + "\"";
            }
        }
        
        returnString += "]";
        
        return returnString;
    }
}
